package com.zhu.service.smtp.service.mail.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.client.utils.JSONUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.zhu.service.base.config.MailConstance;
import com.zhu.service.smtp.pojo.Mail;
import com.zhu.service.smtp.service.mail.CodeService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class CodeServiceImpl  implements CodeService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public String getCode(String email) {

        String msgId = UUID.randomUUID().toString();
        String code = random1();
        Mail mail = new Mail();
        mail.setCode(code);
        mail.setEmail(email);
        rabbitTemplate.convertAndSend(MailConstance.MAIL_QUEUE_NAME, mail, new CorrelationData(msgId));
        return code;
    }

    private static String random1() {
        StringBuilder stringBuilder = new StringBuilder();
        // 使用 Random随机生成数
        Random random = new Random();
        while (stringBuilder.length() < 6) {
            // 定义6位长度，每次随机生成一位数字，存在stringBuilder中
            stringBuilder.append(random.nextInt(10));
        }
        return stringBuilder.toString();
    }






}
